import { _decorator, Component, Node, Label, Vec3 } from 'cc';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
const { ccclass, property } = _decorator;

@ccclass('FinalUILayer')
export class FinalUILayer extends Component {
    @property(Node)
    winTip: Node = null;

    @property(Node)
    panel: Node = null;

    @property(Node)
    title: Node = null;

    @property(Label)
    countDown: Label = null;

    _maxCount = 10;
    _curCount = 0;
    _curt = 0;
    _isUsed = false;

    onLoad() {
        EventManager.on(EventTypes.CurLevelEvents.EnterParty, this.onEnterParty, this);
        EventManager.on(EventTypes.CurLevelEvents.EnterPass, this.onEnterPass, this);

    }

    onEnable() {
        this.winTip.active = false;
        this.panel.active = false;
        this._isUsed = false;
        this.title.active = false;
        this._curCount = this._maxCount;
        this._curt = 0;
    }

    update(dt) {
        if (this.panel.active && this._curCount > 0) {
            this._curt += dt;
            if (this._curt >= 1) {
                this._curt = 0;
                this._curCount--;
                this.countDown.string = this._curCount + '';
                this.countDown.node.active = false;
                this.countDown.node.active = true;

                if (this._curCount <= 0) {
                    this.countDownFinished();
                }
            }
        }
    }

    countDownFinished() {
        this.panel.active = false;
        this.title.active = false;
        EventManager.emit(EventTypes.CurLevelEvents.PartyFinished);
    }

    onEnterParty() {
        if (this._isUsed) return;
        AudioSystem.playBGM(AudioEnum.partyBgm);

        this._isUsed = true;
        this.panel.active = true;
        this.title.active = true;
        this._curCount = this._maxCount;
        this.countDown.string = this._maxCount + '';
        this._curt = 0;
        let d = { t: GlobalEnum.Effect2DType.EnterParty, p: Vec3.ZERO, delay: this._maxCount };
        EventManager.emit(EventTypes.EffectEvents.showUIEffect, d);
    }

    onEnterPass() {
        this.winTip.active = true;
        setTimeout(() => {
            this.winTip.active = false;
        }, 3000);
    }
}

